package in.muton.edushare.pages.batch;

import in.muton.edushare.newentities.BatchUserInviter;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.wicket.PageParameters;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Batches;
import com.edushare.common.vo.FilesUploaded;

public class BatchBulkEntry extends InviteUserPage {

	private static Log log = LogFactory.getLog(BatchBulkEntry.class);
	String[] fields = {"Student Name", "Enrolment Number", "E-mail Id"};
	private IModel<FilesUploaded> fileUploadedModel;
	private IModel<Batches> batchModel;
	
	public BatchBulkEntry(IModel<FilesUploaded> fileUploadedModel, IModel<Batches> batchModel) {
		super(batchModel);
		this.fileUploadedModel = fileUploadedModel;
		this.batchModel = batchModel;
		loadPanels();
	}

	private void loadPanels() {
		try {
			add(new ExcelFormDisplayPanel("loadExcel",fileUploadedModel,4,1){
				private static final long serialVersionUID = 5997853590859490830L;

				@Override
				protected void onCancel() {
					PageParameters params = new PageParameters();
					params.add("0", ""+batchModel.getObject().getBatchId());
					setResponsePage(BatchPeople.class, params);
				}

				@Override
				protected void processSpreadSheet(List<String[]> fields) {
					new BatchUserInviter(getSessionORMInterface(), batchModel).addBulkUsersFromExcelSheet(fields);
					PageParameters params = new PageParameters();
					params.add("0", ""+batchModel.getObject().getBatchId());
					setResponsePage(BatchSummary.class, params);
				}
				
			});
		} catch (InvalidFormatException e) {
			log.error("Illegal File Type", e);
		} catch (IOException e) {
			log.error("RW Exception", e);
		}
	}

	@Override
	public String getPageTitle() {
		return "Enter User Information";
	}
}
